class Solution {
public:
    //https://gitee.com/link?target=https%3A%2F%2Fleetcode.cn%2Fproblems%2Flongest-substring-without-repeating-characters%2F
    int lengthOfLongestSubstring(string s) 
    {
        int arr[128] = {0};
        int left = 0, right = 0;
        int count = 0;
        while(right < s.size())
        {
            arr[s[right]]++;
            while(arr[s[right]] == 2)
            {
                arr[s[left]]--;
                left++;
            }
            count = max(count, right - left + 1);//在这里判断不存在有重复字符子串的情况
            right++;
        }
        return count;
    }

    //https://gitee.com/link?target=https%3A%2F%2Fleetcode.cn%2Fproblems%2Fmax-consecutive-ones-iii%2F
    int longestOnes(vector<int>& nums, int k) 
    {
        int count = 0;
        int left = 0, right = 0;
        int zero = 0;
        while(right < nums.size())
        {
            if(nums[right] == 0) zero++;
            while(zero > k)
            {
                if(nums[left++] == 0) zero--;//left依次++更好控制
            }
            count = max(count, right - left + 1);
            right++;
        }
        return count;
    }

    //https://gitee.com/link?target=https%3A%2F%2Fleetcode.cn%2Fproblems%2Fminimum-operations-to-reduce-x-to-zero%2F
    int minOperations(vector<int>& nums, int x) 
    {
       int sum = 0;
       for(auto& a: nums) sum+=a;
       if(sum < x) return -1;
       int target = sum - x, count = 0;
       int left = 0, right = 0;
       int len = 0;
       if(target == 0) return nums.size();//此时说明sum正好等于x
       while(right < nums.size())
       {
            count += nums[right++];
            while(count >= target)
            {   
                if(count == target) len = max(len, right - left);
                count -= nums[left++];
            }
       }
       return len == 0 ? -1:nums.size() - len;
    }

};